package com.jianguo.web.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.order.domain.OrderParam;
import com.jianguo.order.domain.PzOrder;
import com.jianguo.order.service.IPzOrderDetailService;
import com.jianguo.order.service.IPzOrderService;
import com.jianguo.user.domain.PayParam;
import com.jianguo.user.domain.PzBalance;
import com.jianguo.user.domain.PzMoney;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.service.IPzBalanceService;
import com.jianguo.user.service.IPzMoneyService;
import com.jianguo.user.service.IPzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/api/balance")
public class BalanceApi {

    @Autowired
    private IPzBalanceService balanceService;
    @Autowired
    private IPzUserService pzUserService;
    @Autowired
    private IPzOrderService orderService;
    @Autowired
    private IPzOrderDetailService orderDetailService;
    @Autowired
    private IPzMoneyService moneyService;

    /**
     * 支付
     * @return
     */
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody PayParam payParam){
        if (ObjectUtil.isEmpty(payParam)){
            return AjaxResult.error();
        }
        // 根据密码，userid找到余额
        PzBalance balance = balanceService.getOne(new LambdaQueryWrapper<PzBalance>()
                .eq(PzBalance::getPassword, payParam.getPassword())
                .eq(PzBalance::getUserid, payParam.getUserid()));
        // 余额为空，可能密码错误
        if (ObjectUtil.isEmpty(balance)){
            return AjaxResult.error();
        }
        if (balance.getBalance() < payParam.getMoney()){
            return AjaxResult.error("余额不足");
        }
        // 更改余额、userid
        balance.setUserid(payParam.getUserid())
                .setBalance(balance.getBalance() - payParam.getMoney());
        // 余额更改成功，更改订单状态
        if (balanceService.saveOrUpdate(balance)){
            UpdateWrapper<PzOrder> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_number", payParam.getOrderNumber()); // 设置查询条件
            updateWrapper.set("status", 0); // 设置要更新的字段及值
            updateWrapper.set("pay_method", "余额");
            updateWrapper.set("paytime", DateUtil.date());
            orderService.update(updateWrapper);
        }
        // 余额明细
        PzMoney money = BeanUtil.copyProperties(payParam, PzMoney.class);
        money.setUserid(payParam.getUserid()).setTransactionType("0");

        return AjaxResult.success(moneyService.save(money));
    }
}
